package com.man.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.man.entity.Date;
import com.man.entity.File;

public class FileDao {
	static String URL="jdbc:mysql://localhost:3306/manmanage?user=root&password=root";
	public Boolean insert(File file )
	{
		String sql="insert into file  values(?,?,?,?,?,?) ";
		boolean b=false;
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1, file.getUserName());
			statement.setString(2,file.getTitle());
			statement.setString(3, file.getName());
			statement.setString(4, file.getContentType());
			statement.setString(5,file.getSize());
			statement.setString(6, file.getFilepath());
		    statement.execute();
		    b=true;	
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return b;	
	}
	public List<File> Select(File file) 
	{	List<File> list= new ArrayList<File>();
		String sql="select * from file where userName=? ";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,file.getUserName());
			ResultSet set=	statement.executeQuery();
			while(set.next())
			{	File file2=new File();
				file2.setTitle(set.getString("title"));
				file2.setName(set.getString("name"));
				file2.setContentType(set.getString("contentType"));
				file2.setSize(set.getString("size"));
				list.add(file2);
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
		
	}
	public File oneSelect(File file) 
	{	
		
		File file2=null;
		String sql="select * from file where userName=? and title=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,file.getUserName());
			statement.setString(2,file.getTitle());
			ResultSet set=	statement.executeQuery();
			
			while(set.next())
			{	
				file2=new File();
				file2.setFilepath(set.getString("filepath"));
				file2.setContentType(set.getString("contentType"));
				file2.setName(set.getString("name"));
			}
			
		}  catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return file2;	
	}
	public boolean delete(File file) 
	{
		Boolean b=false;
		String sql="delete from file where userName=? and title=?";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			Connection con=(Connection) DriverManager.getConnection(URL);
			PreparedStatement statement=con.prepareStatement(sql);
			statement.setString(1,file.getUserName());
			statement.setString(2,file.getTitle());
			statement.execute();
			b=true;		
			}
		catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return b;
	}
}
